#ifndef _WORK_CHANNEL_954B90DE_3503_4C3D_B381_A23C60AA7C0E_
#define _WORK_CHANNEL_954B90DE_3503_4C3D_B381_A23C60AA7C0E_
#include "base/message_loop.h"
#include "base/command_line.h"
#include "base/process_util.h"
#include "base/path_service.h"
#include "base/threading/non_thread_safe.h"
#include "base/string_number_conversions.h"
#include "ipc/ipc_listener.h"
#include "ipc/ipc_channel.h"
#include "common/defines.h"
#include "proxy.h"
#include "utils.h"

/**
 * 封装进程间管道操作的辅助模板类。功能如下:
 * a. 实现管道监听接口
 * b. 创建管道服务器端
 * c. 创建通信进程(具体程序可由派生类指定),并将创建好的管道名当命令行参数传进去
 */
template<typename T>
class IPC_NO_VTABLE WorkChannelImpl : public IPC::Listener, public base::NonThreadSafe {
public:
	WorkChannelImpl();
	virtual ~WorkChannelImpl();
  /**
   * 创建管道
   * @param message_loop指定管道运行的线程loop
   */
	void Create(MessageLoop* message_loop);
  /**
   * 销毁管道
   */
	void Destroy();

	// IPC::Listener
	virtual bool OnMessageReceived(const IPC::Message& message);
	virtual void OnChannelConnected(int32 peer_pid);
	virtual void OnChannelError();

  // 发送消息
  bool Send(IPC::Message* message);

	std::wstring GetLaunchFile() const;

protected:
	void InternalCreate();
	void InternalCheckCreate();
	bool CreateChannel();
	bool LaunchPeer();
	CommandLine MakeCmdLine();

protected:
	MessageLoop* message_loop_;
	LONG created_;
	std::string channel_id_;
	scoped_ptr<IPC::Channel> channel_;
};

#include "work_channel.inl"

#endif//_WORK_CHANNEL_954B90DE_3503_4C3D_B381_A23C60AA7C0E_